/**
 * 2017年11月16日
 */
package exp.algorithm.sic.feature;

import java.util.Arrays;

import exp.algorithm.sic.OctaveSpace;
import exp.algorithm.sic.TimeSeries;

/**
 * @author Alex
 *
 */
public abstract  class SegmentedSupport implements FeatureVectorMaker {
	public static void main(String args[]){
		TimeSeries ts = new TimeSeries(new float[]{0,1,2,3,4,5,6,7,8,9,1,2,4,1,45,2});
//		int pointX = 4;
//		SegmentedSupport ss = new SegmentedSupport();
//		System.out.println(getSegmentedStartIndex(pointX, ts));
//		System.out.println(findSegmentedStartX(pointX));
//		System.out.println(findSegmentedEndX(pointX,ts.data.length-1));
//		System.out.println(leftMostIndex(pointX));
		for(int i = 0;i<ts.data.length;i++){
			System.out.println(i);
			System.out.println(Arrays.toString(getSegmentedStartIndex(i, ts)));
		}
	}
	
	//M是段长
	protected final static int M =  OctaveSpace.M;
	protected final static int N =  OctaveSpace.N;
	protected final static int PEAKNUM =  2*	N;
	protected final static int regionLength =	N*M;	
	protected static int[][] getSegmentedStartIndex(int pointX,TimeSeries series) {
		//序列中最近一个点的索引
		int widthIndex = series.data.length-1;
		int left = leftMostIndex(pointX);
		left = left<0?findSegmentedStartX(pointX):left;
		int right= rightMostIndex(pointX);
		right = right>widthIndex?findSegmentedEndX(pointX, widthIndex):right;
		int leftNum = (pointX - left +1)/M; 
		int rightNum = (right-pointX +1)/M;
		int bl = leftNum;
		int br = rightNum;
		int res [] = new int[leftNum + rightNum];
		int s = left;
		int idx = 0;
		while(leftNum>0 && s<pointX){
			res[idx] = s;
			s+=M;
			idx ++;
			leftNum -- ;
		}
		s = pointX;
		while(rightNum > 0 && s<widthIndex){
			res[idx]=s;
			s+=M;
			idx ++ ;
			rightNum --;
		}
		
		return new int[][]{res,new int[]{bl,br}};
	}
	
	private static int findSegmentedStartX(int spx){
		int s = spx;
		while(s-M>=-1){
			s-=M;
		}
		++s;
		return s>spx?spx:s;
	}
	
	private static int findSegmentedEndX(int spx,int endX){
		int s = spx;
		while(s+M<=endX+1){
			s+=M;
		}
		--s;
		return s>endX?endX:s;
	}
	
	private static int leftMostIndex(int spx){
		return spx-regionLength+1;
		
	}
	private static int rightMostIndex(int spx){
		return spx+regionLength-1;
	}

}
